package com.wiegand.gui.navigation {
	import com.asual.swfaddress.SWFAddress;
	import com.wiegand.Configuration;
	import com.wiegand.event.ClickEvent;
	import com.wiegand.event.FullscreenEvent;
	import com.wiegand.event.SoundEvent;
	import com.wiegand.facebook.Facebook;
	import com.wiegand.gui.IGuiElement;
	import com.wiegand.gui.button.FullscreenButton;
	import com.wiegand.gui.button.MetaNavButton;
	import com.wiegand.gui.button.SoundButton;
	import com.wiegand.util.ColorRectangle;
	import com.wiegand.util.SharedObjectManager;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author gandy
	 */
	public class FooterNavigation extends Sprite implements IGuiElement {
		private var _buttons : Array;
		private var _xml : XMLList;
		private var _soundBt : SoundButton;
		private var _fulscreenBt : FullscreenButton;

		public function FooterNavigation(xml : XMLList) {
			_xml = xml;
		}
		
		public function init(): void{
			_buttons = new Array();
			var foo_x : int = 0;
			
			for( var i:int=0; i<_xml.children().length(); i++ ){
				if( _xml.@showLine == "true" && i == 0 ){
					addChild(getLine(foo_x));
					foo_x = int(_xml.@hSpace) + 1;
				}
				var item : MetaNavButton = new MetaNavButton(_xml.child(i));
				item.x = foo_x;
				if( _xml.@showLine != "true" ) foo_x = item.x + item.width + int(_xml.@hSpace);
				else{
					addChild(getLine(foo_x + item.width + int(_xml.@hSpace)));
					foo_x = item.x + item.width + int(_xml.@hSpace) + 1 + int(_xml.@hSpace);
				}
				addChild(item);
				_buttons.push(item);
			}
			
			var startVolume : Number = 1;
			var sharedVolume : String = SharedObjectManager.getInstance().getValue("soundvolume");
			if(sharedVolume!=null) startVolume = Number(sharedVolume); 
			
			_soundBt = new SoundButton(startVolume);
			_soundBt.x = foo_x;
			_soundBt.y = 2;
			addChild(_soundBt);
			_soundBt.init();
			
			foo_x += _soundBt.width + int(_xml.@hSpace);
			
			if( _xml.@showLine == "true" ){
				addChild(getLine(foo_x));
				foo_x += int(_xml.@hSpace);
			}
			
			_fulscreenBt = new FullscreenButton();
			_fulscreenBt.x = foo_x;
			_fulscreenBt.y = 2;
			addChild(_fulscreenBt);
			_fulscreenBt.init();
			foo_x += _fulscreenBt.width + int(_xml.@hSpace);
			
			if( _xml.@showLine == "true" ){
				addChild(getLine(foo_x));
			}			
			
			x = stage.stageWidth - width - Configuration.BORDER;
			y = int(_xml.@y);
			
			addListeners();
		}
		
		private function addListeners() : void {
			_soundBt.addEventListener(SoundEvent.CHANGE, action);
			_fulscreenBt.addEventListener(FullscreenEvent.CHANGE, action);
			for( var i:int=0; i<_buttons.length; i++ ){
				MetaNavButton(_buttons[i]).addEventListener(_xml.child(i).@event, action);
			}
		}
		
		private function action(e:Event) : void {
			switch(e.type){
				case SoundEvent.CHANGE :		
					break;
				case FullscreenEvent.CHANGE	: 
					Configuration.getInstance().main.changeDisplayMode( String( FullscreenEvent(e).state) );	
					break;
				case String(_xml.@event):
					//if( e.currentTarget.name )					
					trace("current: "+e.currentTarget.name);
					trace("current: "+e.currentTarget.name);
					trace("current: "+e.currentTarget.name);
					if(e.currentTarget.name == "facebookteilen"){ 
						
						trace("hey... facebooooook action :)");
						
						var user_message:String = "Hello on wie? gand!";
						var attachment:Object = {'name': 'i\'m bursting with joy', 'href': ' http://bit.ly/187gO1', 'caption': '{*actor*} rated the lolcat 5 stars', 'description': 'a funny looking cat','media': [{ 'type': 'image', 'src': 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg', 'href': 'http://bit.ly/187gO1'}]};
						var action_links:Array = [{'text':'Recaption this', 'href':'http://bit.ly/19DTbF'}];
						Facebook.getInstance().streamPublish(user_message, attachment, action_links);
//						Facebook.getInstance().login();
					}
					SWFAddress.setValue( ClickEvent(e).value );
					break;
				default	:		
					break;
			}
		}

		private function getLine( line_x:int ):Sprite{
			return Sprite( ColorRectangle.create( 0xffffff, .5, 1, 9, line_x, 3 ) );
		}
		
		public function open(delay : Number = 0) : void {
		}
		
		public function close(delay : Number = 0) : void {
		}
		
		public function show(delay : Number = 0) : void {
		}
		
		public function hide(delay : Number = 0) : void {
		}
		
		public function destroy() : void {
		}
		
		public function resizeHandler() : void {
			x = stage.stageWidth - width - Configuration.BORDER;
		}	
	}
}
